package com.dy.baf.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.dy.core.entity.DyResponse;
import com.dy.core.utils.ImageUtil;

@Controller
public class ImageController {

	private static Logger logger = Logger.getLogger(ImageController.class);

	@ResponseBody
	@RequestMapping("{module}/{function}/uploadImage")
	public DyResponse uploadImage(HttpServletRequest request, @PathVariable("module")
	String module, @PathVariable("function")
	String function, String fileName) {
		if (fileName == null)
			return createErrorJsonResonse("文件名不能为空");

		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

		String imagePathDir = ImageUtil.getImagePathDir(module, function);
		String imageRealPathDir = ImageUtil.getImageRealPathDir(imagePathDir);

		File imageSaveFile = new File(imageRealPathDir);
		if (!imageSaveFile.exists()) {
			imageSaveFile.mkdirs();
		}

		String[] fileNames = fileName.split(",");
		MultipartFile multipartFile = null;
		File file = null;
		List<ImageInfo> resultList = new ArrayList<ImageInfo>();
		ImageInfo imageInfo = null;
		for (int i = 0;; i++) {
			multipartFile = multiRequest.getFile("file_" + i);
			if (multipartFile == null) {
				break;
			}
			String newFileName = ImageUtil.generateFileName(fileNames[i]);
			file = new File(imageRealPathDir + newFileName);
			try {
				multipartFile.transferTo(file);

				imageInfo = new ImageInfo();
				imageInfo.setId(imagePathDir + newFileName);
				imageInfo.setName(fileNames[i]);
				imageInfo.setFileurl(imageInfo.getId());
				resultList.add(imageInfo);
			} catch (Exception e) {
				logger.error("文件保存失败", e);
			}
		}

		return createSuccessJsonResonse(resultList);
	}

	public DyResponse createSuccessJsonResonse(Object data) {
		DyResponse response = new DyResponse();
		response.setStatus(DyResponse.OK);
		response.setDescription("OK");
		response.setData(data);

		return response;
	}

	public DyResponse createErrorJsonResonse(String errorMsg) {
		DyResponse response = new DyResponse();
		response.setStatus(DyResponse.ERROR);
		response.setDescription(errorMsg);

		return response;
	}
}

class ImageInfo {

	private String id;

	private String name;

	private String fileurl;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFileurl() {
		return fileurl;
	}

	public void setFileurl(String fileurl) {
		this.fileurl = fileurl;
	}
}